'''# built_in'''
import re
import traceback

'''# custom'''
from common import GSDSTORE, PROJECTROOT
from Interface.common_Interface_Class import CommonInterfaceClass


'''辅助工具类'''
class AuxiliaryToolsClass(CommonInterfaceClass):
    
    def __init__(self) -> None:
        super(AuxiliaryToolsClass, self).__init__(False)
    
    '''Common_Interface_Class 主函数'''
    def CIC_Main(self, fileSrc):
        return super(AuxiliaryToolsClass, self).__call__(fileSrc)
    
    '''Common_Interface_Class 接口请求方法'''
    def CIC_callInterface(self, *args, **kwargs):
        return super(AuxiliaryToolsClass, self).call_Interface(*args, **kwargs)
    
    '''接口响应结果数据过渡 取出/写入/还原'''
    def data_Reduction(self, targetFileSrc, targetData, targetKey):
        '''
         参数：
            @param targetFileSrc: 目标文件路径
            @param targetData: 要取出的目标数据
            @param targetKey: 要进行删除的数据key键
        '''
        self.logicFunction_Object.raw_YamlTxt(targetFileSrc, 'w', targetData)

        RESULT_CACHE_DATA = eval(self.logicFunction_Object.raw_YamlTxt(self.RESULT_FILE)); RESULT_CACHE_DATA.pop(targetKey)
        self.logicFunction_Object.raw_YamlTxt(self.RESULT_FILE, 'w', RESULT_CACHE_DATA)  # 还原接口响应结果缓存文件数据


'''接口辅助信息获取'''
class GetInformationClass(AuxiliaryToolsClass):

    def __init__(self) -> None:
        super(GetInformationClass, self).__init__()
        self.__ROLE_FILE = f'{PROJECTROOT}\\data\\cache\\role.txt'  # 主角色信息
        self.__USER_FILE = f'{PROJECTROOT}\\data\\cache\\user.txt'  # OA用户
        self.__RESULT_FILE = f'{PROJECTROOT}\\data\\cache\\response_result.txt'  # 接口响应结果
        self.__GET_ROLE_FILE = f'{PROJECTROOT}\\data\\static\\Role_InterfaceData.yaml'  # “接口数据|接口执行流程”文件
        self.__GET_USER_FILE = f'{PROJECTROOT}\\data\\static\\User_InterfaceData.yaml'  # “接口数据|接口执行流程”文件
    
    '''类主函数'''
    def __call__(self):
        result_role = self.__get_Role_By_Mgr(GSDSTORE['START']['user'])
        result_user = self.__get_User_By_Information()
        return True if result_role and result_user else False

    '''角色信息获取'''
    def __get_Role_By_Mgr(self, MAIN_ROLE):
        '''
         参数：
            @param MAIN_ROLE: 主角色|初始角色 ID
        '''
        __role_data = self.logicFunction_Object.raw_YamlTxt(self.__GET_ROLE_FILE)
        __role_data['Role_Select']['_PARAMETERS']['DATA']['KEYWORD'] = MAIN_ROLE
        self.logicFunction_Object.raw_YamlTxt(self.__GET_ROLE_FILE, 'w', __role_data)  # 修改“接口数据”文件；主角色ID写入

        '''调用结果完成“主角色”检索'''
        try:
            self.CIC_Main(self.__GET_ROLE_FILE)
        except Exception as error:
            print(traceback.format_exc())
            print(f'role-error: {error}'); print('测试主角色信息获取失败')
            return False
        else:
            self.__set_Interface_Role_Operation()
        return True
    
    '''用户信息获取'''
    def __get_User_By_Information(self):
        '''将OA系统中所有的人员信息取出'''
        try:
            self.CIC_Main(self.__GET_USER_FILE)
        except Exception as error:
            print(traceback.format_exc())
            print(f'user-error: {error}'); print('OA系统用户信息获取失败')
            return False
        else:
            self.__set_Interface_User_Operation()
        return True
    
    '''角色信息处理'''
    def __set_Interface_Role_Operation(self):
        '''将“接口响应结果缓存文件”中的数据转存到“主角色信息缓存文件”中；并将文件清空。['data'][0]响应结果固定数据路径'''
        RESULT_CACHE_DATA = eval(self.logicFunction_Object.raw_YamlTxt(self.__RESULT_FILE))
        self.data_Reduction(self.__ROLE_FILE, RESULT_CACHE_DATA['MAIN_ROLE']['data'][0], 'MAIN_ROLE')  # 数据还原

    '''用户信息处理'''
    def __set_Interface_User_Operation(self):
        resultDict = {}; RESULT_CACHE_DATA = eval(self.logicFunction_Object.raw_YamlTxt(self.__RESULT_FILE))
        
        '''数据筛选'''
        userID = re.findall('name="email_select" value="(.+?)"', RESULT_CACHE_DATA['USER_HTML'])
        userName = re.findall('name="email_select".+?<td nowrap align="center">(.+?)</td>', RESULT_CACHE_DATA['USER_HTML'])
        actualName = re.findall('name="email_select".+?<td nowrap align="center">([\u4e00-\u9fa5]+)</td>', RESULT_CACHE_DATA['USER_HTML'])
        
        '''筛选结果json化处理存储'''
        for index, Name in enumerate(actualName):
            resultDict["".join(self.logicFunction_Object.CHS(userName[index]))] = {
                'ID': userID[index], 'UserName': userName[index], 'Name': Name
            }
        RESULT_CACHE_DATA['USER_HTML'] = resultDict
        self.data_Reduction(self.__USER_FILE, RESULT_CACHE_DATA['USER_HTML'], 'USER_HTML')  # 数据还原